<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/ficon.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/icon.png">
  <link rel="mask-icon" href="/images/icon.png" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"bqlin.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":true,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":{"gitalk":{"order":-2}},"activeClass":"gitalk"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false},"motion":{"enable":false,"async":true,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="API处理套路：  方法一般返回值小于0表示失败。 使用上下文连接多个API。  上下文包含大量相关信息。 上下文一般对应的创建与释放方法，且注释里有说明，例如：open-close、alloc-free。  要复用结构体时，调用对应的unref方法，以重置信息。 所有压缩包、未压缩帧操作都要循环操作。">
<meta property="og:type" content="article">
<meta property="og:title" content="FFmpeg基本API：常用操作">
<meta property="og:url" content="https://bqlin.github.io/posts/ffmpeg_api/index.html">
<meta property="og:site_name" content="權咚领域">
<meta property="og:description" content="API处理套路：  方法一般返回值小于0表示失败。 使用上下文连接多个API。  上下文包含大量相关信息。 上下文一般对应的创建与释放方法，且注释里有说明，例如：open-close、alloc-free。  要复用结构体时，调用对应的unref方法，以重置信息。 所有压缩包、未压缩帧操作都要循环操作。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-09-08T00:26:29.000Z">
<meta property="article:modified_time" content="2021-09-08T00:26:29.000Z">
<meta property="article:author" content="權咚">
<meta property="article:tag" content="音视频">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://bqlin.github.io/posts/ffmpeg_api/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>FFmpeg基本API：常用操作 | 權咚领域</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-105338942-1"></script>
    <script>
      if (CONFIG.hostname === location.hostname) {
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'UA-105338942-1');
      }
    </script>


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?98fcf61fd269408d07dcbc1e49311f9a";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="權咚领域" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">權咚领域</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Making app a work of art.</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://bqlin.github.io/posts/ffmpeg_api/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/icon.png">
      <meta itemprop="name" content="權咚">
      <meta itemprop="description" content="🤘🤯👾">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="權咚领域">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          FFmpeg基本API：常用操作
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-09-08 08:26:29" itemprop="dateCreated datePublished" datetime="2021-09-08T08:26:29+08:00">2021-09-08</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/FFmpeg/" itemprop="url" rel="index"><span itemprop="name">FFmpeg</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>5.1k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>5 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>API处理套路：</p>
<ul>
<li>方法一般返回值小于0表示失败。</li>
<li>使用上下文连接多个API。
<ul>
<li>上下文包含大量相关信息。</li>
<li>上下文一般对应的创建与释放方法，且注释里有说明，例如：open-close、alloc-free。</li>
</ul></li>
<li>要复用结构体时，调用对应的unref方法，以重置信息。</li>
<li>所有压缩包、未压缩帧操作都要循环操作。</li>
</ul>
<h2 id="日志系统">日志系统</h2>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;libavuitl/log.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 设置输出的日志等级</span></span><br><span class="line">av_log_set_level(<span class="type">int</span> level);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 打日志，参1一般为NULL，参2：AV_LOG_DEBUG等常量</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">av_log</span>	<span class="params">(<span class="type">void</span>* avcl, <span class="type">int</span> level, <span class="type">const</span> <span class="type">char</span>* fmt, ...)</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 错误码转详细信息</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> av_err2str(errnum) \</span></span><br><span class="line"><span class="meta">     av_make_error_string((char[AV_ERROR_MAX_STRING_SIZE])&#123;0&#125;, AV_ERROR_MAX_STRING_SIZE, errnum)</span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">char</span> *<span class="title function_">av_make_error_string</span><span class="params">(<span class="type">char</span> *errbuf, <span class="type">size_t</span> errbuf_size, <span class="type">int</span> errnum)</span></span><br><span class="line">&#123;</span><br><span class="line">    av_strerror(errnum, errbuf, errbuf_size);</span><br><span class="line">    <span class="keyword">return</span> errbuf;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="流">流</h2>
<h3 id="获取流信息">获取流信息</h3>
<p>获取流信息基本是基于<code>AVFormatContext</code>进行获取的。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 检查是否支持格式</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">avformat_find_stream_info</span><span class="params">(AVFormatContext* ic, AVDictionary** options)</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取指定类型的流索引，只关心前两两个参数即可</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">av_find_best_stream</span><span class="params">(AVFormatContext* ic, <span class="keyword">enum</span> AVMediaType type, <span class="type">int</span> wanted_stream_nb, <span class="type">int</span> related_stream, AVCodec** decoder_ret, <span class="type">int</span> flags)</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 打印格式信息</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">av_dump_format</span><span class="params">(AVFormatContext* ic, <span class="type">int</span> index, <span class="type">const</span> <span class="type">char</span>* url, <span class="type">int</span> is_output)</span>;</span><br></pre></td></tr></table></figure>
<h3 id="基本流操作框架">基本流操作框架</h3>
<ol type="1">
<li>创建并打开输入上下文。<code>avformat_open_input</code></li>
<li>检查输入格式。<code>avformat_find_stream_info</code>、<code>av_dump_format</code></li>
<li>根据路径创建输出上下文。<code>avformat_alloc_output_context2</code></li>
<li>创建流，因为不参与编解码过程，所以拷贝编解码参数。`<code>avformat_new_stream</code>、<code>avcodec_parameters_copy</code>、<code>out_stream-&gt;codecpar-&gt;codec_tag = 0</code></li>
<li>检查输出格式。<code>av_dump_format</code></li>
<li>打开输出IO。<code>avio_open</code></li>
<li>写入头部。<code>avformat_write_header</code></li>
<li>==从输入流中读取并写入输出数据包。==</li>
<li>写入尾部。<code>av_write_trailer</code></li>
<li>释放上面创建的上下文。<code>avformat_close_input</code>、<code>avio_close</code>、<code>avformat_free_context</code></li>
</ol>
<p>流的操作按照FFmpeg的API流程大多只需要更改高亮的步骤，其余的基本是固定流程。数据包读取与写入的过程一般是这样的：</p>
<ol type="1">
<li>创建数据包结构体。<code>av_packet_alloc</code></li>
<li>循环读取帧，进入帧处理。<code>av_read_frame</code>
<ol type="1">
<li>处理数据包细节：<code>pkt.pts</code>、<code>pkt.dts</code>、<code>pkt.duration</code>、<code>pkt.pos</code></li>
<li>写入。<code>av_interleaved_write_frame</code></li>
<li>减少数据包引用。<code>av_packet_unref</code></li>
</ol></li>
<li>释放数据包结构体。<code>av_packet_unref</code></li>
</ol>
<p>要点：</p>
<h4 id="avformatcontext">AVFormatContext</h4>
<p>从输入URL得出格式信息：</p>
<ol type="1">
<li>建立输入格式上下文。<code>avformat_open_input</code></li>
<li>检查格式是否支持。<code>avformat_find_stream_info</code>、<code>av_dump_format</code></li>
</ol>
<p>从输出URL得出格式信息：</p>
<ol type="1">
<li>从输出URL猜测得出。<code>avformat_alloc_output_context2</code></li>
</ol>
<h4 id="avstream">AVStream</h4>
<p>输入流信息是输入格式上下文的信息，且是完整的：</p>
<ol type="1">
<li>找到指定格式的流索引：<code>av_find_best_stream</code></li>
<li>直接取出：<code>fmt_ctx-&gt;streams[audio_idx]</code></li>
<li>外加异步格式检查：<code>assert_condition(in_stream-&gt;codecpar-&gt;codec_type == AVMEDIA_TYPE_AUDIO, "媒体类型不匹配");</code></li>
</ol>
<p>输出流信息则是要自己创建的：</p>
<ol type="1">
<li>根据输出格式上下文创建输出流：<code>avformat_new_stream</code></li>
<li>从输入流拷贝编解码参数到输出流：<code>avcodec_parameters_copy</code></li>
</ol>
<h4 id="aviocontext">AVIOContext</h4>
<p>存在输出格式上下文中，只需要在写入前后开启和关闭即可。<code>avio_open</code>、<code>avio_close</code>。</p>
<h4 id="avpacket">AVPacket</h4>
<ol type="1">
<li>从输入格式上下文读取数据包：<code>av_read_frame</code></li>
<li>交错写入数据包到输出格式上下文：<code>av_interleaved_write_frame</code></li>
</ol>
<h3 id="应用导出音频流视频流">应用：导出音频流/视频流</h3>
<p>可以用两种方式：</p>
<ul>
<li>读取数据包，写入数据包到文件，补充文件头（这里存在要自己实现文件头的逻辑）。</li>
<li>走FFmpeg整个流程。</li>
</ul>
<p>在以上的基本流程上做修改：</p>
<ul>
<li>获取指定媒体类型，并获取输入流。
<ol type="1">
<li><code>av_find_best_stream</code> -&gt; stream_id</li>
<li>in_stream = <code>fmt_ctx-&gt;streams[stream_idx]</code></li>
</ol></li>
<li>循环读取帧，只处理stream_idx匹配的数据包。</li>
</ul>
<h3 id="应用时间裁剪">应用：时间裁剪</h3>
<ul>
<li>读取数据包之前进行跳转，并获取跳转后pts、dts。<code>av_seek_frame</code></li>
<li>读取数据包时：
<ul>
<li>减去起始的pts、dts。</li>
<li>不处理结束时间之后的数据包。<code>av_q2d(in_stream-&gt;time_base) * pts &lt;= end_time</code></li>
</ul></li>
</ul>
<h2 id="编解码">编解码</h2>
<h3 id="编码">编码</h3>
<p>基本步骤：</p>
<ol type="1">
<li>打开编码器。<code>avcodec_find_encoder_by_name</code></li>
<li>设置编码参数。须手动设置，因为没有参照的来源。</li>
<li>打开编码器。<code>avcodec_open2</code></li>
<li>编码。<code>avcodec_encode_video2</code></li>
</ol>
<p>具体步骤：</p>
<ol type="1">
<li>查询编码器，并创建编码上下文。<code>avcodec_find_encoder_by_name</code>、<code>avcodec_alloc_context3</code></li>
<li>设置编码参数。</li>
<li>打开编码器。<code>avcodec_open2</code></li>
<li>创建文件、AVFrame并把编码上下文的参数设置到AVFrame中。</li>
<li>AVFrame分配缓冲区空间。<code>av_frame_get_buffer</code></li>
<li>编码并写入数据。
<ol type="1">
<li>发送帧。<code>avcodec_send_frame</code></li>
<li>循环接收数据包，并写入文件。<code>avcodec_send_frame</code>、<code>fwrite</code></li>
</ol></li>
<li>编码空的AVFrame以刷新编码器。</li>
<li>按需写入结束码。</li>
<li>关闭文件、释放相关资源。</li>
</ol>
<p>编码细节：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">encode_to_file</span><span class="params">(AVCodecContext* enc_ctx,</span></span><br><span class="line"><span class="params">                           AVFrame* frame,</span></span><br><span class="line"><span class="params">                           AVPacket* pkt,</span></span><br><span class="line"><span class="params">                           FILE* outfile)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> ret;</span><br><span class="line">    <span class="keyword">if</span> (!!frame) &#123;</span><br><span class="line">        av_log(<span class="literal">NULL</span>, AV_LOG_INFO, <span class="string">&quot;Send frame %3&quot;</span> PRId64 <span class="string">&quot;\n&quot;</span>, frame-&gt;pts);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    ret = avcodec_send_frame(enc_ctx, frame);</span><br><span class="line">    assert_errnum(ret, <span class="string">&quot;发送编码帧失败&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (ret &gt;= <span class="number">0</span>) &#123;</span><br><span class="line">        ret = avcodec_receive_packet(enc_ctx, pkt);</span><br><span class="line">        <span class="keyword">if</span> (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        assert_errnum(ret, <span class="string">&quot;编码失败&quot;</span>);</span><br><span class="line"></span><br><span class="line">        av_log(<span class="literal">NULL</span>, AV_LOG_INFO, <span class="string">&quot;Write frame %3&quot;</span> PRId64 <span class="string">&quot; (size=%5d)\n&quot;</span>,</span><br><span class="line">               pkt-&gt;pts, pkt-&gt;size);</span><br><span class="line">        fwrite(pkt-&gt;data, <span class="number">1</span>, pkt-&gt;size, outfile);</span><br><span class="line">        av_packet_unref(pkt);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意：这里是直接把编码后的数据包数据直接写到文件中，并没有写头尾。即没有使用格式上下文的AVIOContext。</p>
<p>要点：</p>
<h4 id="avcodec">AVCodec</h4>
<p>通过id和名称查找，后续通过AVCodecContext进行管理。<code>avcodec_find_encoder_by_name</code></p>
<h4 id="avcodeccontext">AVCodecContext</h4>
<ol type="1">
<li>通过AVCodec创建：<code>avcodec_alloc_context3</code></li>
<li>重点在于格式配置，都设置到该上下文中。</li>
<li>打开后才能使用编解码器：<code>avcodec_open2</code></li>
<li>使用完毕后释放：<code>avcodec_free_context</code></li>
</ol>
<p>编码，两层循环：</p>
<ol type="1">
<li>给编码器上下文塞帧：<code>avcodec_send_frame</code>；</li>
<li>循环从编码器上下文获取数据包：<code>avcodec_receive_packet</code></li>
</ol>
<h4 id="avframe">AVFrame</h4>
<ul>
<li>创建与释放：<code>av_frame_alloc</code>、<code>av_frame_free</code></li>
<li>若是自己填充数据，则要先从codec上下文获取格式设置：<code>pix_fmt</code>、<code>width</code>、<code>height</code></li>
<li>填充数据前要分配空间：<code>av_frame_get_buffer</code></li>
<li>填充数据前要确认帧是否可写入：<code>av_frame_make_writable</code></li>
</ul>
<p>设置了格式决定了分配空间的大小以及后续填充数据的方式。</p>
<h3 id="解码">解码</h3>
<p>基本步骤：</p>
<ol type="1">
<li>查找解码器。<code>avcodec_find_decoder</code></li>
<li>从输入拷贝相关解码参数。<code>avcodec_parameters_to_context</code></li>
<li>打开解码器。<code>avcodec_open2</code></li>
<li>解码。<code>avcodec_decode_video2</code></li>
</ol>
<p>具体步骤：</p>
<ol type="1">
<li>打开文件。<code>avformat_open_input</code></li>
<li>检查格式，获取视频流。<code>avformat_find_stream_info</code>、<code>av_dump_format</code>、<code>av_find_best_stream</code></li>
<li>根据读取的流信息查询编解码器并创建对应上下文。<code>avcodec_find_decoder</code>、<code>avcodec_alloc_context3</code></li>
<li>从读取的流信息中拷贝相关的编解码器参数。<code>avcodec_parameters_to_context</code></li>
<li>打开编解码器。<code>avcodec_open2</code></li>
<li>若要转换图像格式，则创建<code>SwsContext</code>。</li>
<li>循环读取帧。<code>av_read_frame</code></li>
<li>循环解码。
<ol type="1">
<li>发送数据包。<code>avcodec_send_packet</code></li>
<li>循环获得解码帧。<code>avcodec_receive_frame</code></li>
</ol></li>
</ol>
<p>解码细节：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">decode</span><span class="params">(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,</span></span><br><span class="line"><span class="params">                   <span class="type">const</span> <span class="type">char</span> *filename)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> ret;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 发送数据包</span></span><br><span class="line">    ret = avcodec_send_packet(dec_ctx, pkt);</span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">&quot;Error sending a packet for decoding\n&quot;</span>);</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (ret &gt;= <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="comment">// 获得解码帧</span></span><br><span class="line">        ret = avcodec_receive_frame(dec_ctx, frame);</span><br><span class="line">        <span class="keyword">if</span> (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">if</span> (ret &lt; <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">&quot;Error during decoding\n&quot;</span>);</span><br><span class="line">            <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;saving frame %3d\n&quot;</span>, dec_ctx-&gt;frame_number);</span><br><span class="line">        fflush(<span class="built_in">stdout</span>);</span><br><span class="line">		</span><br><span class="line">        <span class="comment">// 帧处理业务</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>与编码不同，解码的参数是由之前编码的决定的，所以这里直接把读取的流中拷贝编码器参数。</p>

    </div>

    
    
    
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E9%9F%B3%E8%A7%86%E9%A2%91/" rel="tag"># 音视频</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/swift_interacts_with_c_language/" rel="prev" title="Swift 与C语言交互">
      <i class="fa fa-chevron-left"></i> Swift 与C语言交互
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/ffmpeg_api_source_code_analysis/" rel="next" title="FFmpeg基本API：源码分析">
      FFmpeg基本API：源码分析 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%97%A5%E5%BF%97%E7%B3%BB%E7%BB%9F"><span class="nav-text">日志系统</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%81"><span class="nav-text">流</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96%E6%B5%81%E4%BF%A1%E6%81%AF"><span class="nav-text">获取流信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E6%B5%81%E6%93%8D%E4%BD%9C%E6%A1%86%E6%9E%B6"><span class="nav-text">基本流操作框架</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#avformatcontext"><span class="nav-text">AVFormatContext</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#avstream"><span class="nav-text">AVStream</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#aviocontext"><span class="nav-text">AVIOContext</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#avpacket"><span class="nav-text">AVPacket</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BA%94%E7%94%A8%E5%AF%BC%E5%87%BA%E9%9F%B3%E9%A2%91%E6%B5%81%E8%A7%86%E9%A2%91%E6%B5%81"><span class="nav-text">应用：导出音频流&#x2F;视频流</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BA%94%E7%94%A8%E6%97%B6%E9%97%B4%E8%A3%81%E5%89%AA"><span class="nav-text">应用：时间裁剪</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BC%96%E8%A7%A3%E7%A0%81"><span class="nav-text">编解码</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BC%96%E7%A0%81"><span class="nav-text">编码</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#avcodec"><span class="nav-text">AVCodec</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#avcodeccontext"><span class="nav-text">AVCodecContext</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#avframe"><span class="nav-text">AVFrame</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A7%A3%E7%A0%81"><span class="nav-text">解码</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="權咚"
      src="/images/icon.png">
  <p class="site-author-name" itemprop="name">權咚</p>
  <div class="site-description" itemprop="description">🤘🤯👾</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">84</span>
          <span class="site-state-item-name">文章</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/bqlin" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;bqlin" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/hearingdog" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;hearingdog" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://xiaozhuanlan.com/metal-reference-translation" title="https:&#x2F;&#x2F;xiaozhuanlan.com&#x2F;metal-reference-translation" rel="noopener" target="_blank">Metal内参</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://xiaozhuanlan.com/wwdc21" title="https:&#x2F;&#x2F;xiaozhuanlan.com&#x2F;wwdc21" rel="noopener" target="_blank">WWDC21 内参</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2016 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">權咚</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">279k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">4:14</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/pangu@4/dist/browser/pangu.min.js"></script>

<script src="/js/utils.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'default',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '7ed324dbfd2741c35c0e',
      clientSecret: '75a59e1f6a55202c4a1b0ec3570ad5b5dccbc390',
      repo        : 'bqlin.github.io',
      owner       : 'bqlin',
      admin       : ['bqlin'],
      id          : '30b03e51ea796c6a2f899f38c9528bd6',
        language: 'zh-CN',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","vOffset":-100},"mobile":{"show":false,"scale":0.5},"log":false});</script></body>
</html>
